package com.hm.web.filter;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
//解决后端的跨域问题
@WebFilter(filterName = "Filter_CrossOrigin",urlPatterns = "/*")
public class Filter1 implements Filter {
    public void destroy() {
    }
    @Override
    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {

            HttpServletRequest request1 = (HttpServletRequest) req;
            HttpServletResponse response1 = (HttpServletResponse) resp;
//        request1.getHeader("origin")
//        response1.setHeader("Set-Cookie", response1.getHeader("Set-Cookie") + "; SameSite=None; Secure=true");
            response1.setHeader("Access-Control-Allow-Origin", request1.getHeader("origin"));
            response1.setHeader("Access-Control-Allow-Methods", "POST, GET, PUT, OPTIONS, DELETE");
            response1.setHeader("Access-Control-Max-Age", "3600");
            response1.setHeader("Access-Control-Allow-Headers", "x-requested-with, Content-Type");
            response1.setHeader("Access-Control-Allow-Credentials", "true");
            chain.doFilter(request1, response1);
    }

    public void init(FilterConfig config) throws ServletException {

    }

}
